import time
import pyautogui  # pip install opencv-python pyautogui
import wx 
import sqlite3
import pathlib
import os
import  autoprint_gui
import configparser
from inc import *
root=pathlib.Path(__file__).parent
db=sqlite3.connect(str(root/"yj.db"))
cur = db.cursor()



#conf.write(open(str(root/"config.ini"), "w"))

mouse_x=conf.getint("autoPrint",'mouse_x')
mouse_y=conf.getint("autoPrint",'mouse_y')

dx=['零','一','二','三','四','五','六','七','八','九','十']

sql="select bj from main group by bj"
r=cur.execute(sql)
kc=[]
for i in r.fetchall():
    kc.append(i[0])
print(kc)




'''
try:
    while True:
        print("Press Ctrl-C to end")
        screenWidth, screenHeight = pag.size()  # 获取屏幕的尺寸
        x, y = pyautogui.position()  # 返回鼠标的坐标
        print("Screen size: (%s %s),  Position : (%s, %s)\n" % (screenWidth, screenHeight, x, y))  # 打印坐标

        time.sleep(1)  # 每个1s中打印一次 , 并执行清屏
except KeyboardInterrupt:
    print('end')
'''
def pr(obj):
    if(obj.m_choice1.GetSelection()>0):
        where1="and nj = \'{0}\'".format(str(obj.m_choice1.GetSelection()+6))
    else:
        where1 = ""
    sql="select count(*) from main where bj='{0}' {1}".format(obj.m_choice2.GetStringSelection(),where1)
    r=cur.execute(sql)
    r=r.fetchall()
    x, y = pyautogui.position()
    pyautogui.click(mouse_x, mouse_y)
    time.sleep(0.3)
    pyautogui.hotkey('ctrlleft','p') # 打印 
    pyautogui.typewrite(str(r[0]))   #打印数量
    pyautogui.press("tab")  
    pyautogui.typewrite("1")   #拷贝
    pyautogui.press("tab")  
    pyautogui.typewrite("1")   #份数
    pyautogui.press("tab")  
    pyautogui.typewrite("1")   #起始记录
    pyautogui.press("enter")   #打印
    pyautogui.moveTo(x,y)

class f(autoprint_gui.MyFrame1):
    pass

class p1(autoprint_gui.MyPanel1):
    def __init__(self, parent):
        autoprint_gui.MyPanel1.__init__(self, parent)
        self.m_choice2.SetItems(kc)
    def click1( self, event ):
        if(self.getData(self.m_choice2.GetStringSelection())):
            pr(self)
        event.Skip()

    def findLocation( self, event ):
        global mouse_x
        global mouse_y
        print(str(root/"01.png.data"))
        r_location = pyautogui.locateOnScreen(str(root/"01.png.data"), confidence=0.8, grayscale=True)
        if(r_location!=None):
            mouse_x=r_location[0]+8
            mouse_y=r_location[1]+10
            pyautogui.moveTo(mouse_x,mouse_y)
            conf.set("autoPrint","mouse_x",str(mouse_x))
            conf.set("autoPrint","mouse_y",str(mouse_y))
            conf.write(open(str(root/"config.ini"), "w"))
        else:
            self.ShowMessage("没有找到刷新按钮")
        event.Skip()


    def click2( self, event ):
        if(self.m_choice2.GetSelection()+1==self.m_choice2.GetCount()):
            self.ShowMessage("已经是最后一个考场了")
        else:
            self.m_choice2.SetSelection(self.m_choice2.GetSelection()+1)
            if(self.getData(self.m_choice2.GetStringSelection())):
                pr(self)
        event.Skip()


    def click3( self, event ):
        print(self.m_choice2.GetSelection())
        if(self.getData(self.m_choice2.GetStringSelection())):
            x, y = pyautogui.position()
            pyautogui.click(mouse_x, mouse_y)
            pyautogui.moveTo(x,y)
        
        event.Skip()


    def choiceGrade( self, event ):
        print(self.m_choice1.GetSelection())
        if(self.m_choice1.GetSelection()>0):
            where2="where nj={}".format(self.m_choice1.GetSelection()+6)
        else:
            where2=""
        sql="select bj from main {0} group by bj".format(where2)
        r=cur.execute(sql)
        kc=[]
        for i in r.fetchall():
            kc.append(i[0])
        self.m_choice2.SetItems(kc)
        event.Skip()


    def ShowMessage(self,msg):
        wx.MessageBox(msg, " " ,wx.OK | wx.ICON_INFORMATION) 
        

    def getData(self,kc):
        if(self.m_choice2.GetSelection()==-1):
            self.ShowMessage("请选择班级")
            return False
        print(self.m_choice2.GetSelection())
        print(self.m_choice1.GetSelection())
        if(self.m_choice1.GetSelection()>0):
            where1="and nj = \'{0}\'".format(str(self.m_choice1.GetSelection()+6))
        else:
            where1 = ""
        sql="select kh,name,bj,kc,z,p from main where bj='{kc}' {wh} order by z,p".format(kc=kc,wh=where1)
        r=cur.execute(sql)
        with open(str(root/"output/temp.txt"), 'w') as f:
            f.write('考号,考场,班级,年级,姓名\n')
            n=1
            for i in r.fetchall():
                print(n,i)
                n=n+1
                #f.write("{kh},{name},{lj}年级,{bj}班,{kc}".format(kh=i[0],name=i[1],lj=dx[i[2][0]],bj=i[2][2:],kc=i[3]))
                f.write("{kh},{kc}考场,{bj}班,{lj}年级,{name}\n".format(kh=i[0],name=i[1],lj=dx[int(i[2][0])],bj=i[2][2:],kc=i[3]))
            f.close()
        return True


app = wx.App()
w = f(None)
p = p1(w)

w.Show()
app.MainLoop()
